// pages/buy/buy.js
Page({
    data:{
        sizeStyle:'',
        category:'',
        proId:'',
        categoryId:'',
        
        product:{
            price:'',
            picture:'',
            crossedPrice:'',
            kind:'浅蓝色',
        },
        address:{
            user_name:'',
            address:''
        }
    },
    //地址查询
    queryAddress(){
        var that = this;
        var app = getApp();
        var useId = app.data.useId;
        wx.request({
            url: 'http://localhost:8080/address/search?useId='+useId,
            method:'POST',
            header:{
                'content-type':'application/json'
            },
            success:function(res) {
                console.log(res);
                var orderId = 0;
                for(var index in res.data.data){
                    if(res.data.data[index].order_id == 1){
                        orderId = index;
                        break;
                    }
                }
                that.setData({
                    address:res.data.data[orderId]
                });
                console.log(that.address);
            },
        })
    },
    //地址跳转
    jumpAddress(){
        wx.navigateTo({
          url: '/pages/address/address',
        })
    },
    queryPicture(){
        var that = this;
        wx.request({
            url: 'http://localhost:8080/product/searchById?proId='+that.data.proId,
            method:'POST',
            header:{
                'content-type':'application/json'
            },
            success:function(res) {
                console.log(res);
                that.setData({
                    product:res.data.data
                });
            },
        })
    },
    showSize() {
        var that = this;
        var categoryId = that.data.categoryId;
        
        if(categoryId == 1)//食品
        {
            that.setData({
                sizeStyle:'display:none;',
                category:'口味'
            })
        }
        if(categoryId == 2)//服装
        {
            that.setData({
                sizeStyle:'display:block;',
                category:'颜色'
            })
        }
    },
    jumpPay(){
        var that = this;
        var app = getApp();
        var userId = app.data.useId;
        console.log(app.data.useId);
        wx.request({
            url: 'http://localhost:8080/order/add?userId='+that.data.proId+'&addressId='+userId+'&proId='+userId,
            method:'POST',
            header:{
                'content-type':'application/json'
            },
            success:function(res) {
                console.log(res);
            },
        })
        wx.reLaunch({
          url: '/pages/pay/pay',
        })
    },
    onShow(){
        this.showSize();
        this.queryPicture();
        this.queryAddress();
    },
    onLoad:function(option) {
        this.proId = option.proId;
        this.categoryId = option.categoryId;
        var categoryId = option.categoryId;
        var Id = option.proId;
        this.setData({
            proId:Id,
            categoryId:categoryId
        })
        console.log(this.data.categoryId)
    }
})
